package com.library.www.service.impl;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.library.www.dao.BorrowDao;
import com.library.www.dao.impl.BorrowDaoImpl;
import com.library.www.entity.BeOverdue;
import com.library.www.entity.BorrowBooks;
import com.library.www.entity.Page;
import com.library.www.entity.ReturnTheBook;
import com.library.www.service.BorrowService;
import netscape.javascript.JSObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BorrowServiceImpl implements BorrowService {
    BorrowDao dao=new BorrowDaoImpl();
    /**
     * 借书
     * @param books
     * @return
     */
    @Override
    public boolean findBorrow (BorrowBooks books) {
        int borrow = dao.borrow(books);
        if (borrow>0){
            return true;
        }
        return false;
    }

    /**
     * 还书
     * @param theBook
     * @return
     */
    @Override
    public boolean findReturn (ReturnTheBook theBook) {
        int i = dao.returnThe(theBook);
        if (i>0){
            return true;
        }
        return false;
    }

    /**
     * 是否有借书
     * @param cardId
     * @param bookId
     * @return
     */
    @Override
    public boolean findSelect (int cardId,int bookId) {
        Map map=new HashMap();
        map.put("cardId",cardId);
        map.put("bookId",bookId);
        int borrow = dao.findBorrow(map);
        if (borrow>0){
            return true;
        }
        return false;
    }

    /**
     * 判断是否逾期
     * @param cardId
     * @param bookId
     * @return
     */
    @Override
    public int findDateTime (int cardId, int bookId) {
        Map map=new HashMap();
        map.put("cardId",cardId);
        map.put("bookId",bookId);
        return dao.findDateTime(map);
    }

    @Override
    public int findDay (int cardId, int bookId) {
        Map map=new HashMap();
        map.put("cardId",cardId);
        map.put("bookId",bookId);
        return dao.findDay(map);
    }

//    @Override
//    public Page<BeOverdue> findByPage (String name, Integer currentPage) {
//        //新建分页对象
//        Page<BeOverdue> page=new Page<>();
//        //设置每页显示的行数
//        page.setPageSize(5);
//        //设置当前页码
//        page.setCurrentPage(currentPage);
//        //获取总记录数，调用dao层去查询
//        int i = dao.findSelect(name);
//        //设置总记录数
//        page.setTotalCount(i);
//        //根据当前页查出起始下标
//        Integer a=(currentPage-1)*page.getPageSize();
//        //调用dao查询当前页的数据集合
//        List<BeOverdue> byPage = dao.findByPage(name, a, page.getPageSize());
//        //存入分页对象（泛型集合）
//        page.setList(byPage);
//        //将分页对象返回
//        return page;
//    }

    /**
     * 违约修改
     * @param overdue
     * @return
     */
    @Override
    public boolean findUpdate (BeOverdue overdue) {
        int update = dao.findUpdate(overdue);
        if (update>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean findDelete (int id) {
        int delete = dao.findDelete(id);
        if (delete>0){
            return true;
        }
        return false;
    }

    /**
     * 新增违约记录
     * @param overdue
     * @return
     */
    @Override
    public boolean findInsert (BeOverdue overdue) {
        int insert = dao.findInsert(overdue);
        if (insert>0){
            return true;
        }
        return false;
    }

    /**
     * 违约记录分页
     * @param name
     * @param start
     * @param page
     * @return
     */
    @Override
    public JsonObject page (String name,int start, int page) {

        Gson gosn = new Gson();
        int select = dao.findSelect(name);
        JsonObject jsob = new JsonObject();
        jsob.addProperty("code",0);
        jsob.addProperty("msg","");
        jsob.addProperty("count",select);
        int star=(start-1)*page;
        JsonArray jsarr = new JsonArray();
        Map map=new HashMap();
        map.put("name",name);
        map.put("star",star);
        map.put("page",page);
        List<BeOverdue> byPage = dao.findByPage(map);
        for(int i=0;i<byPage.size();i++){
            jsarr.add(gosn.toJsonTree(byPage.get(i)));
        }
        jsob.add("data",jsarr);
        return jsob;
    }

    /**
     * 查询借书时间
     * @param cardId
     * @param bookId
     * @return
     */
    @Override
    public String findStarTime (int cardId, int bookId) {
        Map map=new HashMap();
        map.put("cardId",cardId);
        map.put("bookId",bookId);
        String starTime = dao.findStarTime(map);
        return starTime;
    }

    /**
     * 还书记录分页
     * @param name
     * @param start
     * @param page
     * @return
     */
    @Override
    public JsonObject findRepay (String name, int start, int page) {
        Gson gson=new Gson();
        int repay = dao.findRepay(name);
        JsonObject jsob=new JsonObject();
        jsob.addProperty("code",0);
        jsob.addProperty("msg","");
        jsob.addProperty("count",repay);
        int str=(start-1)*page;
        JsonArray jsarr=new JsonArray();
        Map map=new HashMap();
        map.put("name",name);
        map.put("start",start);
        map.put("page",page);
        List<BeOverdue> repayPage = dao.findRepayPage(map);
        for (int i=0;i<repayPage.size();i++){
            jsarr.add(gson.toJsonTree(repayPage.get(i)));
        }
        jsob.add("data",jsarr);
        return jsob;
    }
}
